package com.example.clock.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 格式化工具类
 */
public class DateFormatUtil {

    /**
     * 获取格式化后的当前时间
     *
     * @param pattern 时间格式
     * @return
     */
    public static String format(String pattern) {
        return format(pattern, Locale.CHINESE);
    }

    /**
     * 获取自定义的格式化时间
     *
     * @param date
     * @param pattern 格式
     * @return
     */
    public static String format(Date date, String pattern) {
        return format(date, pattern, Locale.CHINESE);
    }

    private static String format(String pattern, Locale locale) {
        return format(new Date(), pattern, locale);
    }

    private static String format(Date date, String pattern, Locale locale) {
        return (new SimpleDateFormat(pattern, locale)).format(date);
    }

    /**
     * 字符串转时间 dateStr->date
     *
     * @param dateString
     * @return
     */
    public static Date parse(String dateString) {
        return DatePatternType.parseStringToDate(dateString);
    }

    /**
     * 字符串转时间 dateStr->date
     *
     * @param dateString
     * @param pattern
     * @return
     */
    public static String parse(String dateString, String pattern) {
        Date date = DatePatternType.parseStringToDate(dateString);
        return (new SimpleDateFormat(pattern, Locale.CHINESE)).format(date);
    }

    /**
     * 描述:获取本地语言
     * 作者:ZhangYi
     * 时间:2015年1月30日 下午1:24:06
     * 参数：(参数列表)
     *
     * @param language
     * @return
     */
    @Deprecated
    public static Locale getLocale(String language) {
        Locale locale = Locale.CHINESE;
        if (StringUtils.isNotEmpty(language)) {
            language = language.toLowerCase();
            if (language.indexOf("en") != -1) {
                locale = Locale.ENGLISH;
            }
            if (language.indexOf("zh_hk") != -1) {
                locale = Locale.TRADITIONAL_CHINESE;
            }
            if (language.indexOf("zh_cn") != -1) {
                locale = Locale.SIMPLIFIED_CHINESE;
            }
        }
        return locale;
    }

    public static class DatePatternType {
        public static final String YEAR = "yyyy";
        public static final String YEAR_SIMPLE = "yy";
        public static final String MONTH = "MM";
        public static final String MONTH_SIMPLE = "M";
        public static final String DATE = "dd";
        public static final String DATE_SIMPLE = "d";
        public static final String HOUR = "HH";
        public static final String HOUR_SIMPLE = "H";
        public static final String MINUTE = "mm";
        public static final String MINUTE_SIMPLE = "m";
        public static final String SECOND = "ss";
        public static final String SECOND_SIMPLE = "s";
        public static final String FORMAT_yyyy = "yyyy";
        public static final String FORMAT_yyyyMM = "yyyyMM";
        public static final String FORMAT_yyyy_MM = "yyyy-MM";
        public static final String FORMAT_yy_MM = "yy-MM";
        public static final String FORMAT_yyMM = "yyMM";
        public static final String FORMAT_yyMMdd = "yyMMdd";
        public static final String FORMAT_yy_MM_dd = "yy-MM-dd";
        public static final String FORMAT_yyyyMMdd = "yyyyMMdd";
        public static final String FORMAT_yyyy_MM_dd = "yyyy-MM-dd";
        public static final String FORMAT_yyyy年MM月dd日 = "yyyy年MM月dd日";
        public static final String FORMAT_yyyyMMddHHmm = "yyyyMMddHHmm";
        public static final String FORMAT_yyyyMMdd_HH_mm = "yyyyMMdd HH:mm";
        public static final String FORMAT_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
        public static final String FORMAT_yyyyMMddHHmmss = "yyyyMMddHHmmss";
        public static final String FORMAT_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
        public static final String FORMAT_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
        public static final String FORMAT_yyyy_MM_dd_HH_mm_ss_SSS = "yyyy-MM-dd HH:mm:ssSSS";
        public static final String FORMAT_MM_yy = "MM/yy";
        public static final String FORMAT_MM_dd = "MM-dd";
        public static final String FORMAT_MMM = "MMM";
        public static final String FORMAT_MMMM = "MMMM";
        public static final String FORMAT_MM_dd_HH_mm = "MM-dd HH:mm";

        public DatePatternType() {
        }

        public static Date parseStringToDate(String dateString) {
            if (dateString != null && dateString.trim().length() > 0) {
                dateString = dateString.replaceAll("[\\/|\\.]+", "-");
                List<String> formatList = new ArrayList();
                formatList.add("yyyy");
                formatList.add("yyyyMM");
                formatList.add("yyyy-MM");
                formatList.add("yyyy/MM");
                formatList.add("yy-MM");
                formatList.add("yyMM");
                formatList.add("yyMMdd");
                formatList.add("yy-MM-dd");
                formatList.add("yyyyMMdd");
                formatList.add("yyyy-MM-dd");
                formatList.add("yyyy/MM/dd");
                formatList.add("yyyy年MM月dd日");
                formatList.add("yyyyMMddHHmm");
                formatList.add("yyyyMMdd HH:mm");
                formatList.add("yyyy-MM-dd HH:mm");
                formatList.add("yyyyMMddHHmmss");
                formatList.add("yyyy-MM-dd HH:mm:ss");
                formatList.add("yyyy/MM/dd HH:mm");
                formatList.add("yyyy/MM/dd HH:mm:ss");
                formatList.add("yyyyMMddHHmmssSSS");
                formatList.add("yyyy-MM-dd HH:mm:ssSSS");
                formatList.add("MM-dd");
                formatList.add("MM-dd HH:mm");
                formatList.add("HH:mm:ss");

                Date date = null;
                Iterator var3 = formatList.iterator();

                while (var3.hasNext()) {
                    String pattern = (String) var3.next();
                    if ((dateString.indexOf("-") <= 0 || pattern.contains("-")) && (dateString.contains("-") || pattern.indexOf("-") <= 0) && dateString.length() <= pattern.length()) {
                        try {
                            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
                            dateFormat.setTimeZone(TimeZone.getDefault());
                            date = dateFormat.parse(dateString, new ParsePosition(0));
                        } catch (Exception var6) {
                        }
                        if (date != null) {
                            break;
                        }
                    }
                }
                return date;
            } else {
                return null;
            }
        }
    }

}
